package cn.quickcode.monitor.core;

import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.progress.UIJob;

import cn.quickcode.monitor.model.Dbm;
import cn.quickcode.monitor.model.ModelSuppert;
import cn.quickcode.monitor.model.Stm;
import cn.quickcode.monitor.ui.provider.SMConsoleProvider;

public class Task<T extends ModelSuppert> implements Runnable
{
	private String id;

	private String name;

	private TreeViewer treeViewer;

	private long initialDelay;
	
	private long delay;
	
	private TimeUnit timeUnit;
	
	private TaskUnit taskUnit;
	
	private Future<?> future;
	
	public boolean cancel()
	{
		if(this.future != null)
		{
			return this.future.cancel(false);
		}
		return false;
	}

	/**
	 * @return the name
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	public void setDelay(long delay)
	{
		this.delay = delay;
	}

	/**
	 * @return the delay
	 */
	public long getDelay()
	{
		return delay;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void run()
	{
		for (final T task : (List<T>) Task.this.treeViewer.getInput())
		{
			if(task instanceof Dbm)
			{
				SMConsoleProvider.println("Dbm");
			}
			if(task instanceof Stm)
			{
				SMConsoleProvider.println("Stm");
			}
			
			SMConsoleProvider.println(task.getStatus());
			if (task.getStatus().equals("OK"))
			{
				task.setStatus("ER");
			}
			else if (task.getStatus().equals("ER"))
			{
				task.setStatus("WR");
			}
			else if (task.getStatus().equals("WR"))
			{
				task.setStatus("OF");
			}
			else if (task.getStatus().equals("OF"))
			{
				task.setStatus("WC");
			}
			else if (task.getStatus().equals("WC"))
			{
				task.setStatus("OK");
			}
			new UIJob("dbm")
			{
				@Override
				public IStatus runInUIThread(IProgressMonitor monitor)
				{
					Task.this.treeViewer.update(task, new String[] { "status" });
					return Status.OK_STATUS;
				}
			}.schedule(0);
			try
			{
				Thread.sleep(100);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * @return the treeViewer
	 */
	public TreeViewer getTreeViewer()
	{
		return treeViewer;
	}

	/**
	 * @param treeViewer
	 *            the treeViewer to set
	 */
	public void setTreeViewer(TreeViewer treeViewer)
	{
		this.treeViewer = treeViewer;
	}

	/**
	 * @return the id
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id)
	{
		this.id = id;
	}

	/**
	 * @return the future
	 */
	public Future<?> getFuture()
	{
		return future;
	}

	/**
	 * @param future the future to set
	 */
	public void setFuture(Future<?> future)
	{
		this.future = future;
	}

	/**
	 * @return the initialDelay
	 */
	public long getInitialDelay()
	{
		return initialDelay;
	}

	/**
	 * @param initialDelay the initialDelay to set
	 */
	public void setInitialDelay(long initialDelay)
	{
		this.initialDelay = initialDelay;
	}

	/**
	 * @return the timeUnit
	 */
	public TimeUnit getTimeUnit()
	{
		return timeUnit;
	}

	/**
	 * @param timeUnit the timeUnit to set
	 */
	public void setTimeUnit(TimeUnit timeUnit)
	{
		this.timeUnit = timeUnit;
	}

	/**
	 * @return the taskUnit
	 */
	public TaskUnit getTaskUnit()
	{
		return taskUnit;
	}

	/**
	 * @param taskUnit the taskUnit to set
	 */
	public void setTaskUnit(TaskUnit taskUnit)
	{
		this.taskUnit = taskUnit;
	}
}
